package org.linlinjava.litemall.admin.service;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallBill;
import org.linlinjava.litemall.db.domain.LitemallBrandInfo;
import org.linlinjava.litemall.db.domain.LitemallBrandInvoice;
import org.linlinjava.litemall.db.service.LitemallBrandInfoService;
import org.linlinjava.litemall.db.service.LitemallBrandInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * @author:  商户票据信息如果有自动加载
 * @date: 2019/12/12
 * @description:
 */
@Service
public class BrandInvoiceService {
    @Autowired
    private LitemallBrandInvoiceService litemallBrandInvoiceService;
    @Autowired
    private LitemallBrandInfoService brandInfoService;
    /**
     * 票据信息  ：状态 已提交SUBMIT--200  * 未提交UNSUBMIT--300
     */
    private static final Short SUBMIT = 200;
    private static final Short UNSUBMIT = 300;

    /**
     * 创建一个与商户的brandInvoice。没有数据：：bill结算单，创建。。有数据。读最后一个Invoice信息创建
     * @return
     */
    public LitemallBrandInvoice create(LitemallBill bill){
        //票据显示商家信息
        Integer brandId= bill.getBrandId();
        String brandName = bill.getBrandName();
        Integer billId = bill.getId();
        //商户地址、电话 在info中
        LitemallBrandInfo brandInfo = brandInfoService.getInfoByBrandId(brandId);
        String brandAddress = brandInfo.getLocation();
        String phone =brandInfo.getContactPhone();

   /*     //原来思路:根据商户ID 拿到以前票据信息  。//如果没有拿到 与商户的票据信息..是第一次.从bill中拿信息 返回
       创建票据:每次拿到的bill心有 有用的只有
        1结算单号billId,2商户ID,3商户名,4商户地址,5电话.
        所以不需要判断之前有没有保存信息.直接创建
        */

        LitemallBrandInvoice invoice = new LitemallBrandInvoice();
        //票据信息  ：状态 已提交SUBMIT--200  * 未提交UNSUBMIT--300
            invoice.setBillId(billId);
            invoice.setBrandId(brandId);
            invoice.setBrandName(brandName);
            invoice.setAdddress(brandAddress);
            invoice.setTelephone(phone);
            invoice.setAddTime(LocalDate.now());
            invoice.setStatus(UNSUBMIT);
            litemallBrandInvoiceService.create(invoice);
            return invoice;
        //如果list>0  曾经开过票  最后一个票据(最新票据信息)    返回
    }

    /**
     * 更新票据
     * @param invoiveId 票据信息
     * @param url   图片存放地址
     * @return
     */
    public int updateInvoice(Integer invoiveId,String tax,String url){
        //找到创建好的票据信息
        LitemallBrandInvoice invoice = litemallBrandInvoiceService.findById(invoiveId);
        invoice.setTaxNum(tax);
        invoice.setStatus(SUBMIT);
        invoice.setUrl(url);
        int i = litemallBrandInvoiceService.updateInvoice(invoice);
        return i;
    }

   public LitemallBrandInvoice findInvoiceById(Integer id){
      return litemallBrandInvoiceService.findById(id);
   }

}
